/* */

#include "video_widget.hh"

#include <gtkmm/calendar.h>

#include "util/string.hh"

/* FIXME: Temp for debug */
#include <iostream>

VideoWidget::VideoWidget (intf_thread_t *_p_i) : p_intf (_p_i), m_area (), p_vout (NULL)
{

  Glib::Date date;
  Gtk::Calendar ().get_date (date);

  if (date.get_day_of_year () >= 354)
    pixbuf = Gdk::Pixbuf::create_from_file (gtr (config_GetDataDir ())+"/vlc128x128-christmas.png");
  else
    pixbuf = Gdk::Pixbuf::create_from_file (gtr (config_GetDataDir ())+"/vlc128x128.png");

  m_area.set_pixbuf (pixbuf);

  m_area.signal_button_press_event ().connect (sigc::mem_fun (*this, &VideoWidget::button_press_event));

  /* Vlc Video Handling */
  vlc_mutex_init (p_intf, &lock);

  if (config_GetInt (p_intf, "embedded-video") == true)
   {
    p_intf->pf_request_window = ::DoRequest;
    p_intf->pf_control_window = ::DoControl;
    p_intf->pf_release_window = ::DoRelease;
   }

}

VideoWidget::~VideoWidget ()
{

  vlc_mutex_lock( &lock );

  if (p_vout)
   {
    if (!p_intf->psz_switch_intf)
     {
      if (vout_Control (p_vout, VOUT_CLOSE) != VLC_SUCCESS)
        vout_Control (p_vout, VOUT_REPARENT);
     }
    else
     {
      if (vout_Control (p_vout, VOUT_REPARENT) != VLC_SUCCESS)
        vout_Control (p_vout, VOUT_CLOSE);
     }
  }

  vlc_mutex_unlock (&lock);
  vlc_mutex_destroy (&lock);

}

void*
VideoWidget::request (vout_thread_t *_p_vout, int *pi_x, int *pi_y, unsigned int *pi_width, unsigned int *pi_height)
{

  if (p_vout)
   {
    msg_Dbg (p_intf, "embedded video already in use");
    return NULL;
   }
  
  p_vout = _p_vout;

  m_area.set_preferred_size (pi_width[0], pi_height[0]);

  return (void*)m_area.get_xid ();

}

int
VideoWidget::control (void *p_window, int i_query, va_list args)
{

    int i_ret = VLC_SUCCESS;
    switch( i_query )
    {
        case VOUT_GET_SIZE:
        {
            unsigned int *pi_width  = va_arg( args, unsigned int * );
            unsigned int *pi_height = va_arg( args, unsigned int * );
            *pi_width = m_area.get_width ();
            *pi_height = m_area.get_height ();
            break;
        }
        case VOUT_SET_SIZE:
        {
            unsigned int i_width  = va_arg( args, unsigned int );
            unsigned int i_height = va_arg( args, unsigned int );
            m_area.set_preferred_size (i_width, i_height);
            //emit askVideoToResize( i_width, i_height );
            //emit askUpdate();
            break;
        }
        case VOUT_SET_STAY_ON_TOP:
        {
            //int i_arg = va_arg( args, int );
            //QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
            break;
        }
        default:
            i_ret = VLC_EGENERIC;
            msg_Warn( p_intf, "unsupported control query" );
            break;
    }


  std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl;
    
    return i_ret;

}

void
VideoWidget::release (void *p_win)
{

  p_vout = NULL;

}


bool
VideoWidget::button_press_event (GdkEventButton *event)
{

  if (event->button == 3) // Mouse right button
   {
    std::cout << "Display popup menu" << std::endl;
   }

}


